package com.client.act;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.client.bean.BirdList;
import com.client.bean.BirdMessage;
import com.client.common.SpinnerProgressDialog;
import com.client.common.ValueStack;
import com.client.task.BirdMessagesTask;
import com.clinet.cache.AsyncImageLoader;

public class NameSearchAct extends BaseAct {
	public static final String KEY_QUERY = "";

	public static AsyncImageLoader mAsyncImageLoader = null;

	private Context mContext = NameSearchAct.this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.namesearchact);
		initBottomBar();
		init();
		onclick();
		getData();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mAsyncImageLoader.clearImageMap();
		mAsyncImageLoader.shutdownThreadPool();
	}

	private BirdMessagesTask.Callback callback = null;

	private void getData() {
//		SpinnerProgressDialog.showDialog(this,"正在获取数据,请稍后...");

		mBirdMessagesTask = new BirdMessagesTask(mContext, callback);
		mBirdMessagesTask.execute(mBirdList);
	}

	private BirdMessagesTask mBirdMessagesTask = null;

	private void onclick() {
		onInputNum();
	}

	private void onInputNum() {
		searchkey.addTextChangedListener(new TextWatcher() {
			private CharSequence temp;

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				temp = s;
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (temp.length() > 0) {// 按关键字查
					mBirdList.key = s.toString().trim();
					mBirdMessagesTask = new BirdMessagesTask(mContext, callback);
					mBirdMessagesTask.execute(mBirdList);
				} else// 全查
				{
					mBirdList.key = "";
					mBirdMessagesTask = new BirdMessagesTask(mContext, callback);
					mBirdMessagesTask.execute(mBirdList);
				}
			}
		});

	}

	private EditText searchkey = null;
	private ListView listview = null;

	private void init() {
		callback = new BirdMessagesTask.Callback() {
			@Override
			public void onComplete(final Integer result) {

				SpinnerProgressDialog.dismissDialog();
				switch (result) {
				case 1:
					initListView();

					break;
				case 0:

					break;
				}
			}
		};
		layoutFlater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mAsyncImageLoader = new AsyncImageLoader();
		searchkey = (EditText) findViewById(R.id.searchkey);

	}

	private BirdList mBirdList = new BirdList();
	private LayoutInflater layoutFlater;

	// -----------------------------列表----------------------S
	public void initListView() {
		if (mBirdList == null || mBirdList.getTotalNum() <= 0) {
			return;
		}
		listview = (ListView) findViewById(R.id.listview);
		listview.setDivider(null);
		mListViewAdapter = new TimeListAdapter();
		listview.setAdapter(mListViewAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int location, long arg3) {
				BirdMessage mBirdMessage = mBirdList.getItems().get(location);
				DetaiAct.CURRENT=DetaiAct.SEARCHKEY;
				DetaiAct.isSave=true;
				ValueStack.getInstance().setValue("birdName", mBirdMessage.birdName);
				finish();
			}
		});

	}

	private TimeListAdapter mListViewAdapter = null;

	public class TimeListAdapter extends BaseAdapter {

		public TimeListAdapter() {
			super();

		}

		public int getCount() {
			return mBirdList.getTotalNum();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderListViewItem holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(R.layout.search_list_item,
						null);
				holder = new ViewHolderListViewItem();
				holder.birdName = (TextView) convertView
						.findViewById(R.id.list_birdName);
				holder.birdImage = (ImageView) convertView
						.findViewById(R.id.list_birdImage);
				holder.line = (ImageView) convertView.findViewById(R.id.line);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderListViewItem) convertView.getTag();
			}

			BirdMessage bean = mBirdList.getItems().get(position);
			holder.birdName.setText(bean.birdName);
			String url = bean.birdImage;
			holder.birdImage.setTag(url);
			// 载入图片
			NameSearchAct.mAsyncImageLoader.getListViewLogoItem(mContext,
					bean.birdImage, holder.birdImage, listview);
			return convertView;
		}
	}

	private static class ViewHolderListViewItem {
		public TextView birdName = null;
		public ImageView birdImage = null;
		public ImageView line = null;
	}

	// -----------------------------网格----------------------E
}
